package com.xb.p40_usercontentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyProvider extends ContentProvider {
    MySQLiteHelper dbHelper;
    SQLiteDatabase db;

    //在配置文件中进行配置，外界访问的统一资源定位:  content://com.xb.provider/user
    public static final int USER_DIR=0;//0 表示访问这张表
    public static final int USER_ITEM=1;//1 表示访问这张表下的第一条记录  user/1

    public static final String AUTHORITY="com.xb.provider";
    private static UriMatcher uriMatcher;
    static {
        uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY,"user",USER_DIR);
        uriMatcher.addURI(AUTHORITY,"user/#",USER_ITEM);
    }

    @Override
    public boolean onCreate() {
        //建立数据库
        dbHelper=new MySQLiteHelper(getContext(),"user.db",null,1);
        db=dbHelper.getWritableDatabase();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Cursor cursor=null;
        switch (uriMatcher.match(uri)){
            case USER_DIR:
                cursor=db.query("user",projection,selection,selectionArgs,null,null,null);
                break;
            case USER_ITEM:
                String queryID=uri.getPathSegments().get(1);
                cursor=db.query("user",projection,"_id=?",new String[]{queryID},null,null,null);
                break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {//隐式调用的时候使用
        switch (uriMatcher.match(uri)){
            case USER_DIR:
                return "vnd.android.cursor.dir/vnd.com.xb.provider.user";
            case USER_ITEM:
                return "vnd.android.cursor.item/vnd.com.xb.provider.user";
        }
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        switch (uriMatcher.match(uri)){
            case USER_DIR:
            case USER_ITEM:
                long newUserID=db.insert("user",null,values);
                break;
            default:
                break;
        }
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        int deleteInt=0;
        switch (uriMatcher.match(uri)){
            case USER_DIR:
                deleteInt=db.delete("user",selection,selectionArgs);
                break;
            case USER_ITEM:
                String deleteID=uri.getPathSegments().get(1);//通过切分获取要删除的id
                deleteInt=db.delete("user","_id=?",new String[]{deleteID});
                break;
            default:
                break;
        }
        return deleteInt;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int updateInt=0;
        switch (uriMatcher.match(uri)){
            case USER_DIR:
                updateInt=db.update("user",values,selection,selectionArgs);
                break;
            case USER_ITEM:
                String updateID=uri.getPathSegments().get(1);//通过切分获取要删除的id
                updateInt=db.update("user",values,"_id=?",new String[]{updateID});
                break;
            default:
                break;
        }
        return updateInt;
    }
}
